//
//  QRCodeScanningViewController.m
//  EasyToTesco2.0
//
//  Created by knew on 2017/5/16.
//  Copyright © 2017年 EasyToTesco. All rights reserved.
//

#import "QRCodeScanningViewController.h"
#import "ScanSuccessViewController.h"
#import "DataMD5.h"
#import "TransferViewController.h"

@interface QRCodeScanningViewController ()

@end

@implementation QRCodeScanningViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 注册观察者
    [SGQRCodeNotificationCenter addObserver:self selector:@selector(SGQRCodeInformationFromeAibum:) name:SGQRCodeInformationFromeAibum object:nil];
    [SGQRCodeNotificationCenter addObserver:self selector:@selector(SGQRCodeInformationFromeScanning:) name:SGQRCodeInformationFromeScanning object:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}


- (void)SGQRCodeInformationFromeAibum:(NSNotification *)noti {
    NSString *string = noti.object;
    
    NSLog(@"%@",string);
    if ([string hasPrefix:@"http"]) {
        NSDictionary *dic = [DataMD5 getURLParameters:string];
        if ([dic[@"type"] isEqualToString:@"login"]) {
            ScanSuccessViewController *jumpVC = [[ScanSuccessViewController alloc] init];
            jumpVC.urlString = string;
            
            [self.navigationController pushForm:self.navigationController pushToAndHidden:jumpVC];
        } else if ([dic[@"type"] isEqualToString:@"pay"]) {
            
            if (JudgeInvididualOrSeller) {
                TransferViewController *transfer = [[TransferViewController alloc] init];
                transfer.urlString = string;
                [self.navigationController pushForm:self.navigationController pushToAndHidden:transfer];
            } else {
                [MBProgressHUD showMessage:@"商家无法转账" toView:self.view dimBackground:NO AndCompleteBlock:^(id object) {
                    [self dismissViewControllerAnimated:YES completion:nil];
                }];
            }
            
        } else {
            
        }
    } else { // 扫描结果为条形码
        [MBProgressHUD showMessage:@"扫描失败,请稍后重试" toView:self.view dimBackground:NO AndCompleteBlock:^(id object) {
            [self.navigationController popViewControllerAnimated:YES];
        }];
        //        ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init];
        //        jumpVC.jump_bar_code = string;
        //        [self.navigationController pushViewController:jumpVC animated:YES];
    }
}

- (void)SGQRCodeInformationFromeScanning:(NSNotification *)noti {
    
    
    
    
    SGQRCodeLog(@"noti - - %@", noti);
    NSString *string = noti.object;
    
    NSLog(@"%@",string);
    
    if ([string hasPrefix:@"http"]) {
        
        NSDictionary *dic = [DataMD5 getURLParameters:string];
        
        NSLog(@"%@",dic);
        if ([dic[@"type"] isEqualToString:@"login"]) {
            ScanSuccessViewController *jumpVC = [[ScanSuccessViewController alloc] init];
            jumpVC.urlString = string;
            
            [self.navigationController pushForm:self.navigationController pushToAndHidden:jumpVC];
        } else if ([dic[@"type"] isEqualToString:@"pay"]) {
            
            if (JudgeInvididualOrSeller) {
                TransferViewController *transfer = [[TransferViewController alloc] init];
                transfer.urlString = string;
                [self.navigationController pushForm:self.navigationController pushToAndHidden:transfer];
            } else {
                //弹框
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"商家号不支持转账" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *okay = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [self.navigationController popViewControllerAnimated:YES];
                }];
                
                [alert addAction:okay];
                
                [self presentViewController:alert animated:YES completion:nil];
            }
            
        } else {
            
        }
    } else { // 扫描结果为条形码
        return [self showHint:@"扫描失败,请稍后重试"];
//        ScanSuccessJumpVC *jumpVC = [[ScanSuccessJumpVC alloc] init];
//        jumpVC.jump_bar_code = string;
//        [self.navigationController pushViewController:jumpVC animated:YES];
    }

}





- (void)dealloc {
    SGQRCodeLog(@"QRCodeScanningVC - dealloc");
    [SGQRCodeNotificationCenter removeObserver:self];
}

@end
